<!--
 * @Author: 灰佳小白 1150248441@qq.com
 * @Date: 2022-10-12 10:29:21
 * @LastEditors: 灰佳小白 1150248441@qq.com
 * @LastEditTime: 2022-10-13 15:26:46
 * @FilePath: \briup-ui\src\pages\admin\category\List.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <!-- 产品栏目管理 -->
  <div>
    <!-- 添加按钮 -->
    <div class="header">
      <el-button type="primary" @click="toAdd">添加</el-button>
    </div>
    <!-- 添加按钮结束 -->
    <!-- 表格部分 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" :index="1" label="序号" />
      <el-table-column prop="id" label="栏目编号" width="300" />
      <el-table-column prop="name" label="名称" width="300" />
      <el-table-column prop="icon" label="图标" width="300">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="right">
            <img
              slot="reference"
              :src="scope.row.icon"
              width="30px"
              alt="加载失败"
            >
            <img :src="scope.row.icon" width="200px" alt="">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="num" label="排序号" width="200" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <a href="#" class="toEdit" @click.prevent="toEdit(scope.row)">编辑</a>
          <a href="#" class="toDelete" @click.prevent="toDelete(scope.row.id)">删除</a>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格部分结束 -->
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="visible" @close="toClose">
      <el-form ref="myForm" :rules="rules" :model="form">
        <el-form-item
          label="栏目名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="序号" :label-width="formLabelWidth">
          <el-input v-model="form.num" />
        </el-form-item>
        <el-form-item label="图标" :label-width="formLabelWidth" prop="icon">
          <el-upload
            class="avatar-uploader"
            :action="uploadFileURL"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="form.icon" width="20px" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="toClose">取 消</el-button>
        <el-button type="primary" @click="toSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { uploadFileURL } from '@/utils/config'
import { saveOrUpdateQuery, deleteQuery } from '@/api/proCategory'
export default {
  data() {
    return {
      // 图片上传路径
      uploadFileURL,
      // 表格数据
      tableData: [],
      // 图标变量
      imageUrl: '',
      // 模态框标题
      title: '',
      // 控制模态框显示与隐藏变量
      visible: false,
      // 表单对象
      form: {},
      formLabelWidth: '120px',
      // 表单验证规则
      rules: {
        name: [{ required: true, message: '请输入产品名称', trigger: 'blur' }],
        icon: [{ required: true, message: '请上传图标', trigger: 'change' }]
      }
    }
  },
  computed: {
    ...mapGetters(['columns'])
  },
  created() {
    // 获取栏目分类状态
    this.$store
      .dispatch('column/queryColumns', { page: 1, pageSize: 100 })
      .then(() => {
        this.tableData = this.columns
      })
  },
  mounted() {},
  methods: {
    toAdd() {
      this.title = '添加栏目'
      this.visible = true
      this.form = {}
      this.imageUrl = ''
    },
    toEdit(row) {
      this.title = '编辑栏目'
      this.visible = true
      this.form = { ...row }
      this.imageUrl = this.form.icon
    },
    // 删除栏目
    toDelete(id) {
      // 提示用户
      this.$confirm('此操作将删除该栏目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await deleteQuery({ id })
          console.log(res)
          this.$message({
            type: 'success',
            message: res.message
          })
          this.$store
            .dispatch('column/queryColumns', { page: 1, pageSize: 100 })
            .then(() => {
              this.tableData = this.columns
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    },
    toClose() {
      // 重置表单验证
      setTimeout(() => {
        this.visible = false
        this.form = {}
        this.$refs.myForm.resetFields()
      }, 500)
    },
    toSubmit() {
      // 校验表单
      this.$refs.myForm.validate(async(valid) => {
        if (valid) {
          // 发送保存或更新请求
          const res = await saveOrUpdateQuery(this.form)
          console.log(res)
          this.$notify({
            type: 'success',
            message: res.message
          })
          this.$store
            .dispatch('column/queryColumns', { page: 1, pageSize: 100 })
            .then(() => {
              this.tableData = this.columns
            })
          this.toClose()
        } else {
          return false
        }
      })
    },
    // 图片上传成功的方法
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      // console.log(res);// {msg,data--图片保存在服务器的地址}
      this.form.icon = res.data
    },
    // 图片上传之前的方法
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>
<style lang='scss'>
$color: #e65d6e;
a {
  margin-right: 10px;
}
.toEdit {
  color: #30b08f;
}
.toDelete {
  color: $color;
}
</style>
